<?php
require('../../../config/config.php');
ob_start();
error_reporting(E_ALL);
// SHOW ROUTINES
function show($e){
	$e=ereg_replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;",htmlentities($e));
	echo "<PRE style='color:#000066;font-family:Courier, mono;font-size:11px'>";
	print_r($e);
	echo "</PRE>";
}

$contents = $_POST["pane"];

// initialize variables
$CR = "\r\n";
$package="";
$class="";
$table="";
$php = false;
$mysql = false;
$admin = false;
$author="";
$version="";
$desc=array();
$var=array();


//parse the string
$contents = ereg_replace("\r","",$contents);
$lines = explode("\n",$contents);

for ($i=0;$i<count($lines);$i++){

	if (substr($lines[$i],0,1)=="@"){
		// we have to process something
		$command=explode(":",$lines[$i]);


		switch (strtolower($command[0])){
			case "@php":
				$php=true;
				break;
			case "@mysql":
				$mysql=true;
				break;
			case "@table":
				$table=$command[1];
				break;
			case "@admin":
				$admin=true;
				break;
			case "@package":
				$package=$command[1];
				break;
			case "@class":
				$class=$command[1];
				break;
			case "@author":
				$author=$command[1];
				break;
			case "@version":
				$version =$command[1];
				break;
			case "@desc":
				array_push($desc,$command[1]);
				break;
			case "@var":
				$tmp=array("title"=>$command[1],"name"=>$command[2],"type"=>$command[3],"length"=>$command[4],"default"=>$command[5],"extra"=>$command[6],"PK"=>$command[7]);
				array_push($var,$tmp);
				break;
		}
	}
}
$tmp=array("title"=>"updated_on","name"=>"updated_on","type"=>"TIMESTAMP","length"=>"","default"=>"","extra"=>"","PK"=>"");
array_push($var,$tmp);
$tmp=array("title"=>"created_on","name"=>"created_on","type"=>"TIMESTAMP","length"=>"","default"=>"","extra"=>"","PK"=>"");
array_push($var,$tmp);


@mkdir(DIR_TMP."admin_creator");
@mkdir(DIR_TMP."admin_creator/sql");
@mkdir(DIR_TMP."admin_creator/admin");
@mkdir(DIR_TMP."admin_creator/admin/$package");
@mkdir(DIR_TMP."admin_creator/resource");
@mkdir(DIR_TMP."admin_creator/resource/classes");
@mkdir(DIR_TMP."admin_creator/resource/classes/$package");

// HERE WE CREATE THE CLASS OBJECT
//echo "<HR>";
//echo "THE CLASS FILE: $class.class.php<BR>";
//echo "PACKAGE: $package";
//echo "<HR>";

include("php.creator.php");
//header('Content-type: text/plain');
//header('Content-disposition: attachment; filename="'.$class.'.class.php"');
//show($classFile);
//exit();


$fr = fopen(DIR_TMP."admin_creator/resource/classes/$package/$class.class.php","w");
fwrite($fr,$classFile);
fclose($fr);


//echo "<HR>";
//echo "THE MYSQL FILE";
//echo "<HR>";
include("mysql.creator.php");
$fr = fopen(DIR_TMP."admin_creator/sql/".$package."_".$class.".mysql.sql","w");
fwrite($fr,$mysqlFile);
fclose($fr);

include("admin.list.creator.php");

$fr = fopen(DIR_TMP."admin_creator/admin/$package/$class.list.php","w");
fwrite($fr,$adminList);
fclose($fr);

include("admin.list.yui.creator.php");
$fr = fopen(DIR_TMP."admin_creator/admin/$package/$class.list.yui.php","w");
fwrite($fr,$adminList);
fclose($fr);

include("admin.list.json.creator.php");
$fr = fopen(DIR_TMP."admin_creator/admin/$package/$class.list.json.php","w");
fwrite($fr,$adminList);
fclose($fr);

include("admin.edit.creator.php");
$fr = fopen(DIR_TMP."admin_creator/admin/$package/$class.edit.php","w");
fwrite($fr,$adminEdit);
fclose($fr);

include("admin.process.creator.php");
$fr = fopen(DIR_TMP."admin_creator/admin/$package/$class.process.php","w");
fwrite($fr,$adminProcess);
fclose($fr);

include("admin.delete.creator.php");
$fr = fopen(DIR_TMP."admin_creator/admin/$package/$class.delete.php","w");
fwrite($fr,$adminDelete);
fclose($fr);

include("files.creator.php");
$fr = fopen(DIR_TMP."admin_creator/admin/$package/$class.files.php","w");
fwrite($fr,$filesFile);
fclose($fr);

//ZIPPING PART - Only owrks on linux so far
/*
$myZip = new ZipArchive();
$myZip->addFile(DIR_TMP."admin_creator/admin/$package/$class.files.php","admin/$package/$class.list.php");
$myZip->addFile(DIR_TMP."admin_creator/admin/$package/$class.edit.php","admin/$package/$class.edit.php");
$myZip->addFile(DIR_TMP."admin_creator/admin/$package/$class.process.php","admin/$package/$class.process.php");
$myZip->addFile(DIR_TMP."admin_creator/admin/$package/$class.delete.php","admin/$package/$class.delete.php");
$myZip->addFile(DIR_TMP."admin_creator/admin/$package/$class.files.php","admin/$package/$class.files.php");
$myZip->


if (file_exists(DIR_TMP."$package.$class.tar")) unlink(DIR_TMP."$package.$class.tar");
if (file_exists(DIR_TMP."$package.$class.tar.gz")) unlink(DIR_TMP."$package.$class.tar.gz");
error_reporting(E_ALL);
$tmpDir = rtrim(DIR_TMP,"/");
$FWtmpDir = rtrim(DIR_TEMP_DLD,"/");
echo `tar -C $tmpDir/admin_creator -cf $tmpDir/$package.$class.tar admin/ resource/ sql/ && gzip $tmpDir/$package.$class.tar && rm -Rf $tmpDir/admin_creator && mv -f $tmpDir/$package.$class.tar.gz $FWtmpDir/$package.$class.framework`;
$download_size = filesize("$FWtmpDir/$package.$class.framework");
echo "<a href='".HTTP_TEMP_DLD."$package.$class.framework'>Download file $package.$class.framework ($download_size)</a>";
*/
?>
Files have been saved in toy applications TMP directory.<br/>
Please copy them to the correct locations to start using them.<br/>
For the admin files to appear correctly in you administration system, please include the following line in your:
admin/dyn.menu.php<br />
<br />
include_once('<?php echo $package?>/<?php echo $class?>.files.php');<br />
<br>
<br>

